package rise.message;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import rise.comunity.Status;


public class Message extends Packet {
	private String sender;
	private String receiver;
	private String text;
	private String status;
	
	private static final String SENDER_ATTRIBUTE = "sender";
	private static final String RECEIVER_ATTRIBUTE = "receiver";
	private static final String STATUS_ATTRIBUTE = "status";
	private static final String TEXT_TAG = "text";
	
	public static final int OFFLINE_RECEIVER = 1;
	
	public Message(String sender, String receiver, String text, int port, double lat, double lon, String status) {
		super(PacketType.MESSAGE, port, lat, lon);
		this.sender = sender;
		this.receiver = receiver;
		this.text = text;
		this.status = status;
	}
	
	public Message(String sender, String receiver, String text, int port, double lat, double lon, Status status) {
		this(sender, receiver, text, port, lat, lon, status.toString());
	}	
	
	@Override
	public String toXML(){
		Document doc = null;
		try {
			doc = super.createXMLDocument();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}
		
	    Element root = doc.createElement(rootElementTag());
	    doc.appendChild(root);
	    root.setAttribute(SENDER_ATTRIBUTE, sender);
	    root.setAttribute(RECEIVER_ATTRIBUTE, receiver);
	    root.setAttribute(STATUS_ATTRIBUTE, status);

	    Element textElement = doc.createElement(TEXT_TAG);
	    textElement.setTextContent(text);
	    
	    Element connectionElement = super.createConnectionElement(doc);
    
	    root.appendChild(textElement);
	    textElement.appendChild(connectionElement);
	    
	    String result = null;
	    try {
			result = super.generateString(doc);
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	    return result;
	}

	public boolean isMessage(Document doc){
		return doc.getElementById(this.rootElementTag()) == null;
	}
	
	public static Message createMessage(String xmlString){
		Document doc = Packet.createXMLDocumentFromString(xmlString);

		NodeList list = doc.getElementsByTagName(PacketType.MESSAGE.toString());
		Node rootNode = list.item(0);
		
		list = doc.getElementsByTagName(TEXT_TAG);
		Node textNode = list.item(0);
		
		Node connectionNode = doc.getElementsByTagName(Packet.CONNECTION_TAG).item(0);
		NamedNodeMap connectionAttributes = connectionNode.getAttributes();
		
		NamedNodeMap rootAttributes = rootNode.getAttributes();
		
		return new Message(
				rootAttributes.getNamedItem(SENDER_ATTRIBUTE).getNodeValue(),
				rootAttributes.getNamedItem(RECEIVER_ATTRIBUTE).getNodeValue(),
				textNode.getTextContent(),
				Integer.parseInt(connectionAttributes.getNamedItem(Packet.PORT_ATTRIBUTE).getNodeValue()),
				Double.parseDouble(connectionAttributes.getNamedItem(Packet.LAT_ATTRIBUTE).getNodeValue()),
				Double.parseDouble(connectionAttributes.getNamedItem(Packet.LON_ATTRIBUTE).getNodeValue()),
				rootAttributes.getNamedItem(STATUS_ATTRIBUTE).getNodeValue()
				);
	}
	
	@Override
	String rootElementTag() {
		return PacketType.MESSAGE.toString();
	}
	
	public String getReceiver() {
		return receiver;
	}
	
	public String getSender() {
		return sender;
	}
	
	public String getText() {
		return text;
	}
	
	public boolean isMessage(){
		return true;
	}
	
	public Status getStatus() {
		if(this.status.equals(Status.ONLINE.toString()))
			return Status.ONLINE;
		if(this.status.equals(Status.BUSY.toString()))
			return Status.BUSY;
		return Status.OFFLINE;		
	}
}
